package com.example.toolbar.toolbar;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.toolbar.R;
import com.example.toolbar.databinding.FragmentIcon1Binding;

import java.lang.reflect.Method;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

/***
 * 创建时间：2021/6/2 22:07
 * 创建人：10850
 * 功能描述：
 */
public class IconFragment1 extends Fragment {


    private FragmentIcon1Binding binding;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //View inflate = inflater.inflate(R.layout.fragment_icon1, container, false);
        binding = FragmentIcon1Binding.inflate(inflater, container, false);
        View inflate = binding.getRoot();
        return inflate;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.main_menu, menu);
        if (menu != null) {
            if (menu.getClass() == MenuBuilder.class) {
                try {
                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e) {
                }
            }
        }
//        final MenuItem item = menu.findItem(R.id.menu_custom);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Toolbar toolbar = /*view.findViewById(R.id.ftb);*/ binding.ftb;
        toolbar.setTitle("");
        //只能通过AppCompatActivity类型的
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    }
}